﻿#pragma once
#include <iostream>
using namespace std;

// 枚举颜色
enum Colour
{
	RED,
	BLACK
};

// 红黑树节点
template<class K, class V>
struct RBTNode
{
	pair<K, V> _kv;				// 存储值
	RBTNode<K, V>* _left;		// 左指针
	RBTNode<K, V>* _right;		// 右指针
	RBTNode<K, V>* _parent;     // 父亲指针

	// 颜色
	Colour _col;

	// 列表初始化
	RBTNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{};
};

// 红黑树
template<class K, class V>
class RBTree
{
	using Node = RBTNode<K, V>;
public:

	bool Insert(const pair<K, V>& kv)
	{
		// 红黑树为空树
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}

		// 红黑树不为空树
		// 找到插入位置
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else {
				// 不实现冗余
				return false;
			}
		}

		// 创建新的节点
		cur = new Node(kv);
		cur->_col = RED;
		// 连接孩子
		if (parent->_kv.first < kv.first)
			parent->_right = cur;
		else
			parent->_left = cur;
		// 连接父亲
		cur->_parent = parent;

		// 调整红黑树的颜色
		// 如果插入后父亲为黑色节点直接返回true即可
		while (parent && parent->_col == RED)
		{
			Node* grandpa = parent->_parent;

			if (parent == grandpa->_left)
			{
				Node* uncle = grandpa->_right;
				if (uncle && uncle->_col == RED)
				{
					//     g
					//   p   u
					// 只需变色的情况
					// 该情况不需要考虑cur的位置
					parent->_col = uncle->_col = BLACK;
					grandpa->_col = RED;

					// 向上更新
					cur = grandpa;
					parent = cur->_parent;
				}
				else // uncle不存在或者，uncle的col为BLACK
				{
					// parent在grandpa左边并且cur在parent的左边
					//     g
					//   p   u
					// c
					if (cur == parent->_left)
					{
						// 右单旋+变色
						RotateR(grandpa);
						parent->_col = BLACK;
						grandpa->_col = RED;
					}
					else
					{
						// parent在grandpa左边并且cur在parent的右边
						//     g
						//   p   u
						//     c
						// cur == parent->_right
						// 左右双旋+变色
						RotateL(parent);
						RotateR(grandpa);
						// c变黑，g变红
						cur->_col = BLACK;
						grandpa->_col = RED;
					}
					break;
				}
			}
			else // parent == grandpa->_right
			{
				Node* uncle = grandpa->_left;
				if (uncle && uncle->_col == RED)
				{
					//     g
					//   u   p
					// 只需变色的情况
					// 该情况不需要考虑cur的位置
					parent->_col = uncle->_col = BLACK;
					grandpa->_col = RED;

					// 向上更新
					cur = grandpa;
					parent = cur->_parent;
				}
				else // uncle不存在或者，uncle的col为BLACK
				{
					// parent在grandpa右边并且cur在parent的右边
					//     g
					//   u   p
					//          c
					if (cur == parent->_right)
					{
						// 左单旋+变色
						RotateL(grandpa);
						parent->_col = BLACK;
						grandpa->_col = RED;
					}
					else
					{
						
						// parent在grandpa右边并且cur在parent的左边
						//     g
						//   u   p
						//     c
						// cur == parent->_left
						// 右左双旋+变色
						RotateR(parent);
						RotateL(grandpa);
						// c变黑，g变红
						cur->_col = BLACK;
						grandpa->_col = RED;
					}
					break;
				}
			}
		}
		// 根节点更新为BLACK
		_root->_col = BLACK;
		return true;
	}

	// 中序遍历
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	// 检查平衡
	bool IsBalance()
	{
		if (_root == nullptr)
			return true;
		if (_root->_col == RED)
			return false;
		// 参考值
		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			} 
			cur = cur->_left;
		} 
		return Check(_root, 0, refNum);
	}
private:

	// 右单旋
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* pparent = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;

		if (pparent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
				pparent->_left = subL;
			else
				pparent->_right = subL;
			subL->_parent = pparent;
		}
	}

	// 左单旋
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* pparent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;

		if (pparent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
				pparent->_left = subR;
			else
				pparent->_right = subR;
			subR->_parent = pparent;
		}
	}

	// 中序遍历
	void _InOrder(Node* _root)
	{
		if (_root == nullptr)
			return;
		_InOrder(_root->_left);
		cout << _root->_kv.first << ":" << _root->_kv.second << endl;
		_InOrder(_root->_right);
	}

	// 检查红黑树
	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			// 前序遍历⾛到空时，意味着⼀条路径⾛完了
			//cout << blackNum << endl;
			if (refNum != blackNum)
			{
				cout << "存在⿊⾊结点的数量不相等的路径" << endl;
				return false;
			}
			return true;
		}
		// 检查孩⼦不太⽅便，因为孩⼦有两个，且不⼀定存在，反过来检查⽗亲就⽅便多了
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红⾊结点" << endl;
			return false;
		}
		if (root->_col == BLACK)
		{
			blackNum++;
		}
		return Check(root->_left, blackNum, refNum)
			&& Check(root->_right, blackNum, refNum);
	}

	Node* _root = nullptr;
};
